<?php
class Attachment extends Controller {
	function Attachment(){
		parent::Controller();	
	}
	function image(){
		$config['upload_path'] = './sknimg/i-see-attr/imageAttachment';
		$config['allowed_types'] = 'jpg|png|gif';
		$config['max_size'] = '0';
		$config['max_width'] = '0';
		$config['max_height'] = '0';
		$config['overwrite'] = FALSE;
		$this->load->library('upload', $config);
		
		$data ['noError'] = $this->upload->do_upload('imgatt');
		$data ['errorMsg'] =htmlspecialchars(strip_tags($this->upload->display_errors()));
		
		if($data ['noError']){
			$uploadData = $this->upload->data();
			$this->load->helper('file');
			$config['image_library'] = 'gd2';
			$config['source_image'] = realpath("./sknimg/i-see-attr/imageAttachment/".$uploadData['file_name']."");
			$config['create_thumb'] = FALSE;
			$config['maintain_ratio'] = TRUE;
			$config['width'] = 150;
			$config['height'] = 150;
			$config['new_image'] = './sknimg/i-see-attr/imageAttachmentThumb/'.$uploadData['file_name'];
			$this->load->library('image_lib', $config);
			$data ['noError'] = $this->image_lib->resize();
			$data ['errorMsg'] = $this->image_lib->display_errors();
			$data['filename'] = $uploadData['file_name'];
			
			if($uploadData['image_width']>640){
				$config['image_library'] = 'gd2';
				$config['source_image'] = realpath("./sknimg/i-see-attr/imageAttachment/".$uploadData['file_name']."");
				$config['create_thumb'] = FALSE;
				$config['maintain_ratio'] = TRUE;
				$config['width'] = 640;
				$config['new_image'] = './sknimg/i-see-attr/imageAttachment/';
				$this->load->library('image_lib', $config);
				
				$this->image_lib->resize();
			}
		}	
		echo json_encode($data);
	}
	
	function deleteImage(){
		$this->load->helper('file');
		$fileName = $this->input->post('fileName');
		delete_file(realpath('./sknimg/i-see-attr/imageAttachment'),$fileName);
		delete_file(realpath('./sknimg/i-see-attr/imageAttachmentThumb'),$fileName);
	}
	
	function file(){
		$config['upload_path'] = './sknimg/i-see-attr/fileAttachment';
		$config['allowed_types'] = 'txt|doc|ppt|zip|rar|docx|pptx';
		$config['max_size'] = '30000';
		$config['overwrite'] = FALSE;
		$this->load->library('upload', $config);
		
		$data ['noError'] = $this->upload->do_upload('fileatt');
		$data ['errorMsg'] =htmlspecialchars(strip_tags($this->upload->display_errors()));
		
		if($data ['noError']){
			$uploadData = $this->upload->data();
			$data['filename'] = $uploadData['file_name'];
		}
		echo json_encode($data);
	}
	
	function deleteFile(){
		$this->load->helper('file');
		$fileName = $this->input->post('fileName');
		delete_file(realpath('./sknimg/i-see-attr/fileAttachment'),$fileName);
	}
}